#!/usr/bin/perl

use lib::abs qw( .. );
use JIP::ToolSet; # load my commonly-used modules in a single import

use English qw( -no_match_vars );
use Test::More tests => 21;

use_ok('Log::Console');
require_ok('Log::Console');

can_ok(
    'Log::Console',
    qw(
        new
        Note
        Debug
        Warn
        Error
        Fatal
        _logLevel
        _outputHandle
        _outputBuffering
        _getTimeStamp
        _writeMSG
        _getLogMessage
        _isAvailableMessage
    )
);

{
    my $log = Log::Console->new(
        log_level => 'error',
        buffering => 1,
    );

    isa_ok( $log, 'Log' );
    isa_ok( $log, 'Log::Console' );
    isa_ok( $log, 'Class::Base' );
    isa_ok( $log, 'Class::Accessor::Fast' );

    is( $log->_outputBuffering, 1,       'outputBuffering accessor' );
    is( $log->_logLevel,        'error', 'logLevel accessor' );

    try {
        $log->_logLevel = 'tratata';
    }
    catch {
        like(
            $ARG,
            qr/^Can't modify non-lvalue subroutine call at/,
            'r.o. access to _logLevel'
        );
    };

    try {
        $log->_outputHandle = 'tratata';
    }
    catch {
        like(
            $ARG,
            qr/^Can't modify non-lvalue subroutine call at/,
            'r.o. access to _outputHandle'
        );
    };

    try {
        $log->_outputBuffering = 'tratata';
    }
    catch {
        like(
            $ARG,
            qr/^Can't modify non-lvalue subroutine call at/,
            'r.o. access to _outputBuffering'
        );
    };

    is( $log->_isAvailableMessage('fatal'), 'fatal', 'fatal - _isAvailableMessage method' );
    is( $log->_isAvailableMessage('error'), 'error', 'error - _isAvailableMessage method' );
    is( $log->_isAvailableMessage('note'),  undef,   'note  - _isAvailableMessage method' );

    like(
        $log->_getLogMessage( message_type => 'N', message_text => 'tratata' ),
        qr/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w\s{5}:\stratata$/x,
        "'N' log message"
    );

    like(
        $log->_getLogMessage( message_type => 'DEBUG', message_text => 'tratata' ),
        qr/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\sDEBUG\s:\stratata$/x,
        "'DEBUG' log message"
    );

    like(
        $log->_getLogMessage( message_type => 'DEBUGGG', message_text => 'tratata' ),
        qr/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\sDEBUGGG\s:\stratata$/x,
        "'DEBUGGG' log message"
    );

    like(
        $log->_getTimeStamp,
        qr/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/x,
        'time_stamp'
    );
}

{
    my $log;

    try {
        $log = Log::Console->new( buffering => 1 );
    }
    catch{
        like(
            $ARG,
            qr/^required parameter "log_level" not defined at /,
            'constructor error'
        );
    };

    is( $log, undef, 'emty class' );
}
